/**
 * components util
 */

/**
 * 清理空值，对象
 * @param children
 * @returns {*[]}
 */
export function filterEmpty(children = []) {
  return children.filter((c: any) => c.tag || (c.text && c.text.trim() !== ''))
}

/**
 * 获取字符串长度，英文字符 长度1，中文字符长度2
 * @param {*} str
 */
export function getStrFullLength(str = '') {
  return str.split('').reduce((pre, cur) => {
    const charCode = cur.charCodeAt(0)
    if (charCode >= 0 && charCode <= 128)
      return pre + 1

    return pre + 2
  }, 0)
}

/**
 * 截取字符串，根据 maxLength 截取后返回
 * @param {*} str
 * @param {*} maxLength
 */
export function cutStrByFullLength(str = '', maxLength: number) {
  let showLength = 0
  return str.split('').reduce((pre, cur) => {
    const charCode = cur.charCodeAt(0)
    if (charCode >= 0 && charCode <= 128)
      showLength += 1
    else
      showLength += 2

    if (showLength <= maxLength)
      return pre + cur

    return pre
  }, '')
}

// 全屏封装
export function launchIntoFullscreen(element) {
  if (element.requestFullscreen)
    element.requestFullscreen()
  else if (element.mozRequestFullScreen)
    element.mozRequestFullScreen()
  else if (element.webkitRequestFullscreen)
    element.webkitRequestFullscreen()
  else if (element.msRequestFullscreen)
    element.msRequestFullscreen()
}
// 退出全屏封装
export function exitFullscreen() {
  if (document.exitFullscreen)
    document.exitFullscreen()
  // else if (document.mozCancelFullScreen)
  //   document.mozCancelFullScreen()
  // else if (document.webkitExitFullscreen)
  //   document.webkitExitFullscreen()
}
